/**
 * @author jprieur
 * @created 7 juin 2007
 */
package mygui;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

import results.*;
import kernel.*;
import objects.*;

public class MonitorPanel extends JPanel
{
    private HashMap<Traceable,ProbeTable> _objects;
    private JScrollPane _scroll;
    private ProbeTable _current;

    public MonitorPanel()
    {
	super(new BorderLayout());
	_objects = new HashMap<Traceable,ProbeTable>();
	_scroll = new JScrollPane();
	add("Center", _scroll);
    }

    public void clear()
    {
	_objects.clear();
	_scroll.setViewportView(new JLabel());
    }

    public void display(Traceable object)
    {
	ProbeTable table = _objects.get(object);
	_current = table;
	table.update();
	_scroll.setViewportView(table);
    }

    public void update()
    {
	if(_current == null) return;
	_current.update();
    }

    public void addObject(Traceable object)
    {
	_objects.put(object, new ProbeTable(object));
    }

    public void distributeProbe(Probe probe)
    {
	Traceable owner = probe.getLinkedStation();
	ProbeTable table = _objects.get(owner);
	table.addProbe(probe);
    }

    public class ProbeTable extends JTable
    {
	private Traceable _object;
	private DefaultTableModel _model;
	private Vector<Probe> _probes;

	public ProbeTable(Traceable object)
	{
	    _object = object;
	    _probes = new Vector<Probe>();
	    _model = new DefaultTableModel();
	    setModel(_model);
	}

	public void addProbe(Probe probe)
	{
	    _probes.add(probe);
	}

	public void update()
	{
	    String[] headers = { "Probe", "Value" };
	    Object[][] data = new Object[_probes.size()][2];
	    for(int i = 0;i < _probes.size();i++){
		Probe current = _probes.get(i);
		data[i][0] = current.getClass().getName().substring(13);
		data[i][1] = current.getBonoboValue();
	    }
	    _model.setDataVector(data, headers);
	}

	public void addNotify()
	{
	    super.addNotify();
	    update();
	}

	public String toString()
	{
	    return _probes.toString();
	}
    }

}